home *** CD-ROM | disk | FTP | other *** search
/ Cre@te Online 2000 December / Cre@teOnline CD05.iso / MacSoft / XML ConsoleMax.sea / XML ConsoleMax / Required / ccs_util.jar / com / commerceone / util / urn / URN.class (.txt) < prev   
Encoding:
Java Class File  |  1999-12-09  |  5.8 KB  |  228 lines

  1. package com.commerceone.util.urn;
  2.  
  3. import java.io.Serializable;
  4.  
  5. public class URN implements Serializable {
  6.    private String nid;
  7.    private String nss;
  8.  
  9.    public String getNSS() {
  10.       return this.nss;
  11.    }
  12.  
  13.    private static boolean isValidNSSCharStatic(char nssChar) {
  14.       return nssChar == '%' || nssChar >= 'A' && nssChar <= 'Z' || nssChar >= 'a' && nssChar <= 'z' || nssChar >= '0' && nssChar <= '9' || nssChar >= '\'' && nssChar <= '.' || nssChar == '!' || nssChar == '$' || nssChar == ':' || nssChar == ';' || nssChar == '=' || nssChar == '@' || nssChar == '_';
  15.    }
  16.  
  17.    public String toString() {
  18.       return new String("urn:" + this.nid + ":" + this.nss);
  19.    }
  20.  
  21.    public URN(String urnString) throws MalformedURNException {
  22.       this.checkForValidURN(urnString);
  23.       String testNID = this.extractNIDFromString(urnString);
  24.       if (testNID == null) {
  25.          throw new MalformedURNException("Bad or missing NID.");
  26.       } else {
  27.          String testNSS = this.extractNSSFromString(urnString);
  28.          if (testNSS == null) {
  29.             throw new MalformedURNException("Bad or missing NSS.");
  30.          } else {
  31.             this.nid = testNID;
  32.             this.nss = testNSS;
  33.          }
  34.       }
  35.    }
  36.  
  37.    public URN(String nidString, String nssString) throws MalformedURNException {
  38.       if (!this.isValidNID(nidString)) {
  39.          throw new MalformedURNException("Bad or missing NID.");
  40.       } else if (!this.isValidNSS(nssString)) {
  41.          throw new MalformedURNException("Bad or missing NSS.");
  42.       } else {
  43.          this.nid = nidString;
  44.          this.nss = nssString;
  45.       }
  46.    }
  47.  
  48.    public int hashCode() {
  49.       return this.nid.hashCode() * this.nss.hashCode();
  50.    }
  51.  
  52.    public static String decodeString(String inString) {
  53.       StringBuffer outStringBuffer = new StringBuffer(inString.length());
  54.  
  55.       char testChar;
  56.       for(int pos = 0; pos < inString.length(); outStringBuffer.append(testChar)) {
  57.          testChar = inString.charAt(pos++);
  58.          if (testChar == '%') {
  59.             testChar = (char)Integer.parseInt(inString.substring(pos, pos + 2), 16);
  60.             pos += 2;
  61.          }
  62.       }
  63.  
  64.       return new String(outStringBuffer);
  65.    }
  66.  
  67.    private String extractNIDFromString(String urnString) throws MalformedURNException {
  68.       this.checkForValidURN(urnString);
  69.       String nidAndNss = urnString.substring(4);
  70.       int pos = nidAndNss.indexOf(58);
  71.       if (pos == -1) {
  72.          return null;
  73.       } else {
  74.          String nidString = nidAndNss.substring(0, pos);
  75.          return this.isValidNID(nidString) ? nidString : null;
  76.       }
  77.    }
  78.  
  79.    private boolean isValidNIDChar(char nidChar) {
  80.       return nidChar == '-' || nidChar >= 'A' && nidChar <= 'Z' || nidChar >= 'a' && nidChar <= 'z' || nidChar >= '0' && nidChar <= '9';
  81.    }
  82.  
  83.    private boolean isValidNSS(String nssString) throws MalformedURNException {
  84.       if (nssString == null) {
  85.          return false;
  86.       } else {
  87.          int nssLength = nssString.length();
  88.          if (nssLength < 1) {
  89.             return false;
  90.          } else {
  91.             for(int pos = 0; pos < nssLength; ++pos) {
  92.                char testChar = nssString.charAt(pos);
  93.                if (!this.isValidNSSChar(testChar)) {
  94.                   return false;
  95.                }
  96.  
  97.                if (testChar == '%') {
  98.                   if (pos + 2 >= nssLength || !this.isValidHexChar(nssString.charAt(pos + 1)) || !this.isValidHexChar(nssString.charAt(pos + 2))) {
  99.                      return false;
  100.                   }
  101.                } else if (this.isReservedChar(testChar)) {
  102.                   throw new MalformedURNException("unescaped reserved character '" + testChar + "' used.");
  103.                }
  104.             }
  105.  
  106.             return true;
  107.          }
  108.       }
  109.    }
  110.  
  111.    private boolean isReservedChar(char rawChar) {
  112.       return rawChar == '/' || rawChar == '?' || rawChar == '#';
  113.    }
  114.  
  115.    public String getNID() {
  116.       return this.nid;
  117.    }
  118.  
  119.    private void checkForValidURN(String urnString) throws MalformedURNException {
  120.       if (urnString != null && urnString.length() >= 7) {
  121.          if (!urnString.startsWith("urn:") && !urnString.startsWith("URN:")) {
  122.             throw new MalformedURNException("Bad or missing URN prefix.");
  123.          }
  124.       } else {
  125.          throw new MalformedURNException("Bad or missing URN.");
  126.       }
  127.    }
  128.  
  129.    public boolean equals(Object obj) {
  130.       if (!(obj instanceof URN)) {
  131.          return false;
  132.       } else {
  133.          String thisNID = this.nid.toLowerCase();
  134.          String thatNID = ((URN)obj).getNID().toLowerCase();
  135.          String thisNSS = this.nss.toLowerCase();
  136.          String thatNSS = ((URN)obj).getNSS().toLowerCase();
  137.          return thisNID.equals(thatNID) && thisNSS.equals(thatNSS);
  138.       }
  139.    }
  140.  
  141.    public boolean equalsString(String string) {
  142.       try {
  143.          URN newURN = new URN(string);
  144.          return this.equals(newURN);
  145.       } catch (MalformedURNException var3) {
  146.          return false;
  147.       }
  148.    }
  149.  
  150.    private String extractNSSFromString(String urnString) throws MalformedURNException {
  151.       this.checkForValidURN(urnString);
  152.       String nidAndNss = urnString.substring(4);
  153.       int pos = nidAndNss.indexOf(58);
  154.       if (pos == -1) {
  155.          return null;
  156.       } else if (pos == nidAndNss.length() - 1) {
  157.          return null;
  158.       } else {
  159.          String nssString = nidAndNss.substring(pos + 1);
  160.          return this.isValidNSS(nssString) ? nssString : null;
  161.       }
  162.    }
  163.  
  164.    private boolean isValidNSSChar(char nssChar) {
  165.       return nssChar == '%' || nssChar >= 'A' && nssChar <= 'Z' || nssChar >= 'a' && nssChar <= 'z' || nssChar >= '0' && nssChar <= '9' || nssChar >= '\'' && nssChar <= '.' || nssChar == '!' || nssChar == '$' || nssChar == ':' || nssChar == ';' || nssChar == '=' || nssChar == '@' || nssChar == '_';
  166.    }
  167.  
  168.    private boolean isValidHexChar(char rawChar) {
  169.       return rawChar >= 'A' && rawChar <= 'F' || rawChar >= 'a' && rawChar <= 'f' || rawChar >= '0' && rawChar <= '9';
  170.    }
  171.  
  172.    private static String encodeChar(char rawChar) {
  173.       String hexString = Integer.toHexString(rawChar);
  174.       hexString = hexString.toUpperCase();
  175.       return hexString.length() == 1 ? "%0" + hexString : '%' + hexString;
  176.    }
  177.  
  178.    private boolean isValidNID(String nidString) throws MalformedURNException {
  179.       if (nidString == null) {
  180.          return false;
  181.       } else {
  182.          int nidLength = nidString.length();
  183.          if (nidLength < 1) {
  184.             return false;
  185.          } else {
  186.             for(int pos = 0; pos < nidLength; ++pos) {
  187.                char testChar = nidString.charAt(pos);
  188.                if (!this.isValidNIDChar(testChar)) {
  189.                   return false;
  190.                }
  191.  
  192.                if (testChar == '%') {
  193.                   if (pos + 2 >= nidLength || !this.isValidHexChar(nidString.charAt(pos + 1)) || !this.isValidHexChar(nidString.charAt(pos + 2))) {
  194.                      return false;
  195.                   }
  196.                } else if (this.isReservedChar(testChar)) {
  197.                   throw new MalformedURNException("unescaped reserved character '" + testChar + "' used.");
  198.                }
  199.             }
  200.  
  201.             return true;
  202.          }
  203.       }
  204.    }
  205.  
  206.    public boolean sameNID(URN otherURN) {
  207.       String thisNID = this.nid.toLowerCase();
  208.       String thatNID = otherURN.getNID().toLowerCase();
  209.       return thisNID.equals(thatNID);
  210.    }
  211.  
  212.    public static String encodeString(String inString) {
  213.       StringBuffer outStringBuffer = new StringBuffer("");
  214.       int pos = 0;
  215.  
  216.       while(pos < inString.length()) {
  217.          char testChar = inString.charAt(pos++);
  218.          if (!isValidNSSCharStatic(testChar)) {
  219.             outStringBuffer.append(encodeChar(testChar));
  220.          } else {
  221.             outStringBuffer.append(testChar);
  222.          }
  223.       }
  224.  
  225.       return new String(outStringBuffer);
  226.    }
  227. }
  228.